#include "panel.h"

Panel::Panel( bool resizable )
{
	isPanel = true;
	isTouchable = true;
	isTransparant = true;
	isAlwaysOnTop = false;
	isResizable = resizable;
	zaxis = 0;
	if ( isResizable )
	{
		Widget* w = addWidgetButton( "resizebutton", Vector2i( 0, 0 ), Vector2i(10, 10), "", Vector2i(0, 0), cmd.gen(""), EVENT_NOREPEAT, 0 );
		w->isAlignedRight = true;
		w->isAlignedBottom = true;
	}
}

void Panel::draw()
{
	if (active)
	{
		drawBackground();
		drawBorders();
		drawChildren();
	}
}

void Panel::drawBackground()
{
	if (isTransparant)
	{
		glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
		glEnable(GL_BLEND);
	}

	glPushMatrix();
	glColor4f(0.05f, 0.05f, 0.05f, 0.75f);
// 	glTranslatef(0.5f, 0.5f, 0.0f); // pixel precision offset
	glBegin(GL_QUADS);
		glVertex2f(absPosition.x,         absPosition.y+v_height);
		glVertex2f(absPosition.x,         absPosition.y);
		glVertex2f(absPosition.x+v_width, absPosition.y);
		glVertex2f(absPosition.x+v_width, absPosition.y+v_height);
	glEnd();
	glPopMatrix();

	if (isTransparant)
		glDisable(GL_BLEND);
}

void Panel::drawBorders()
{
	glPushMatrix();
	glColor3f(0.5f, 0.5f, 0.5f);
// 	glTranslatef(0.5f, 0.5f, 0.0f); // pixel precision offset
	glBegin(GL_LINES);
		glVertex2f(absPosition.x,         absPosition.y+v_height);
		glVertex2f(absPosition.x,         absPosition.y);

		glVertex2f(absPosition.x,         absPosition.y);
		glVertex2f(absPosition.x+v_width, absPosition.y);

		glVertex2f(absPosition.x+v_width, absPosition.y);
		glVertex2f(absPosition.x+v_width, absPosition.y+v_height);

		glVertex2f(absPosition.x+v_width, absPosition.y+v_height);
		glVertex2f(absPosition.x,         absPosition.y+v_height);
	glEnd();
	glPopMatrix();
}

Panel::~Panel()
{
}
